<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

if(!defined('TinyFW')) exit('Access denied!');

//配置文件处理档
class tfConfig {


    private $config;
    private static $instance;

    private function __construct() {
        $this->_load_config();
    }
    
    public function __clone() {}

    public static function singleton() {
        if(! isset(self::$instance)) {
            self::$instance = new self(); 
        }
        return self::$instance;
    }

    //载入配置,app的配置将覆写system的配置
    private function _load_config() {
        //echo "load config";
        $this->_load_sys_config();
        $this->_load_app_config();
        $this->_load_mod_config();
    }

    //載入系統配置檔
    private function _load_sys_config() {

        if(file_exists(BASE_PATH . "config".DS . "config.ini.php")) {
            include_once(BASE_PATH . "config".DS."config.ini.php");
            if(is_array($config)) {
                $this->config = $config;
            }
            unset($config);
        }
    }

    //載入應用配置檔
    private function _load_app_config() {
        if(file_exists(APP_PATH . tfUri::getAppName() .DS . "config" . DS . "config.ini.php")) {
            include_once(APP_PATH . tfUri::getAppName() .DS . "config" . DS . "config.ini.php");
            if(is_array($config)) {
                $this->config = array_merge($this->config, $config);
            }
            unset($config);
        }
    }
    //載入module的配置檔
    private function _load_mod_config() {
        if(file_exists(APP_PATH . tfUri::getAppName() .DS . "modules" . DS . tfUri::getModuleName() . DS . "config" . DS . "config.ini.php")) {
            include_once(APP_PATH . tfUri::getAppName() .DS . "modules" . DS . tfUri::getModuleName() . DS . "config" . DS . "config.ini.php");
            if(is_array($config)) {
                $this->config = array_merge($this->config, $config);
            }
            unset($config);
        }
    }

    private function get($param) {
        if(isset ($this->config[$param])) {
            return $this->config[$param];
        }
        return null;
    }

    private function set($param, $value) {
        $this->config[$param] = $value;
    }

    public static function getConfig($param) {
        //return self::$config[$param];
        return self::singleton()->get($param);

    }

    //允许用户在程序执行过程中动态更改配置内容
    public static function setConfig($param, $value) {
        //self::$config[$param] = $value;
        self::singleton()->set($param, $value);
    }

    /*public static function getConfigs() {
        return self::$config;
    } */
    
}

?>
